package crudfx.util;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.io.*;
import java.awt.*;
/**
 * @profile desktop
 * @author Sergey Surikov
 */
public class DropCatcher{
    public-init var component:Component;
    public-init var onDrop:function(text:String[]):Void;
    init{
        var dropTargetListener:DropTargetListener=DropTargetListener{
            override public function dragEnter(evt:DropTargetDragEvent):Void { }
            override public function dragOver(evt:DropTargetDragEvent):Void {  }
            override public function dragExit(evt:DropTargetEvent ):Void {}
            override public function dropActionChanged(evt:DropTargetDragEvent):Void {}
            override public function drop(evt:DropTargetDropEvent):Void {
                var values:String[];
                try{
                    var t:Transferable=evt.getTransferable();
                    var s:String="";
                    if (t.isDataFlavorSupported(java.awt.datatransfer.DataFlavor.javaFileListFlavor)) {
                        evt.acceptDrop(DnDConstants.ACTION_COPY);
                        var list:java.util.List=t.getTransferData(java.awt.datatransfer.DataFlavor.javaFileListFlavor) as java.util.List;
                        var cnt=0;
                        var sz=list.size();
                        var file:File;
                        while(cnt<sz){
                            file=list.get(cnt) as File;
                            s=file.getAbsolutePath();
                            insert s into values;
                            cnt++;
                            }
                        evt.getDropTargetContext().dropComplete(true);
                        }
                    else if (t.isDataFlavorSupported(java.awt.datatransfer.DataFlavor.stringFlavor)) {
                        evt.acceptDrop(DnDConstants.ACTION_COPY);
                        s=t.getTransferData(java.awt.datatransfer.DataFlavor.stringFlavor).toString();
                        evt.getDropTargetContext().dropComplete(true);
                        insert s into values;
                    } else {
                        evt.rejectDrop();
                        }
                    if(onDrop!=null)onDrop(values);
                }catch(x){x.printStackTrace();}
                }
            };
        var dropTarget:DropTarget=new DropTarget(component, dropTargetListener);
        }
    }
